package AMT;

import java.util.ArrayList;
import java.util.HashMap;

public class UserDao {
    //定义一些实现功能的方法
    //判断是否存在该用户，如果存在 返回true，如果不存在 返回false登录失败
    public static boolean login(String id, String password, HashMap map){
        boolean flag = false;
        User user = (User)map.get(id);
        String pwd = user.getPassword();
        if(pwd.equals(password)){
            flag = true;
        }
        return flag;
    }

    public static int changePassword(String id,String password,HashMap map){
        User user = (User)map.get(id);
        user.setPassword(password);
        return 0;
    }

    public static int SaveMoney(String id,double money,HashMap map){
        User user = (User)map.get(id);
        money = money + user.getMoney();
        user.setMoney(money);
        return 0;
    }

    public static double QueryMoney(String id,HashMap map){
        User user = (User)map.get(id);
        double money = user.getMoney();
        return money;
    }

    public static double WithdrawMoney(String id,double money,HashMap map){
        User user = (User)map.get(id);
        double OldMoney = user.getMoney();
        if(OldMoney >= money){
            OldMoney = OldMoney - money;
            //修改map里面的余额
            user.setMoney(OldMoney);
        }else {
            System.out.println("取钱失败！");
        }
        //返回余额
        return QueryMoney(id,map);
    }

    //添加记录
    public static int AddRecord(String id,Record record,HashMap map){
        User user = (User)map.get(id);
        ArrayList<Record> arrayList = user.getList();
        //将记录放入集合
        arrayList.add(record);
        return 0;
    }
    //查询记录
    public static ArrayList<Record> QueryRecord(String id,HashMap map){
        User user = (User)map.get(id);
        ArrayList<Record> arrayList = user.getList();
        return arrayList;
    }
}
